package com.artisan.admin.controllers;

import com.artisan.admin.model.adViewModels.AdViewModel;
import com.artisan.admin.resultModels.MessageBase;
import com.artisan.domain.dto.AdDto;
import com.artisan.domain.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import java.io.IOException;

/** 开始控制器 */
@RequestMapping("/api/hello")
@RestController
@Api("开始模块")
@ApiOperation(value = "开始模块")
public class HelloController {
  private final FileService fileService;

  public HelloController(FileService fileService) {
    this.fileService = fileService;
  }
  /** 上传文件 */
  // POST /upload
  @ApiOperation(value = "添加广告")
  @ApiResponses(@ApiResponse(code = 200, response = MessageBase.class, message = "成功"))
  @RequestMapping(
      value = {"/upload"},
      method = {RequestMethod.POST})
  @ResponseBody
  public MessageBase postUpload(@RequestPart("file") MultipartFile file) throws IOException {
    MessageBase result = new MessageBase();

    String fileName = fileService.storeFile(file);

    //        String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath()
    //                .path("/downloadFile/")
    //                .path(fileName)
    //                .toUriString();

    result.setData(fileName);

    return result;
  }
}
